package com.hx.vending.controller;

import com.hx.vending.bean.Dict;
import com.hx.vending.service.IDictService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/25.
 */
@Controller
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private IDictService dictService;
    /**
     * 新增父级字典
     */
    @RequestMapping(value = "/addParentDict")
    @ResponseBody
    public Msg add(@RequestBody Dict dict) {
        Dict d = dictService.selectByPrimaryKey(dict);
        if(ToolUtil.isEmpty(d)){
            dict.setPid(0);
            dict.setNum(0);
            int i = dictService.insertSelective(dict);
            if(i > 0){
                return Msg.success();
            }
        }else{
            return Msg.fail().add("msg","该字典已存在");
        }
        return Msg.fail();
    }

    /**
     * 查询所有父级字典
     * @return
     */
    @RequestMapping("/selectParentDict")
    @ResponseBody
    public Msg selectParentDict(){
        Dict dict = new Dict();
        dict.setPid(0);
        List<Dict> dictList = dictService.selectByExample(dict);
        return Msg.success().add("dictList",dictList);
    }

    @RequestMapping("/addDict")
    @ResponseBody
    public Msg addDict(@RequestBody Dict dict){
        Dict d = new Dict();
        d.setPid(dict.getPid());
        List<Dict> dictList = dictService.selectByExample(d);
        dict.setNum(dictList.size() + 1);
        int i = dictService.insertSelective(dict);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 修改字典
     * @param dict
     * @return
     */
    @RequestMapping("/updateDict")
    @ResponseBody
    public Msg updateDict(@RequestBody Dict dict){
        int i = dictService.updateByPrimaryKey(dict);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 删除字典
     */
    @RequestMapping(value = "/deleteDict/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteDict(@PathVariable("ids") String ids){
        Integer i = 0;
        if(ids.equals("-")){
            String[] strs = ids.split("-");
            for(int j = 0;j < strs.length;j++){
                i += delete(Integer.parseInt(strs[j]));
            }
        }else{
             i = this.delete(Integer.parseInt(ids));
        }
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    public Integer delete(Integer id){
        Dict d = new Dict();
        d.setId(id);
        Dict dict = dictService.selectByPrimaryKey(d);
        Integer i = 0;
        if(dict.getPid().equals(0)){
            Dict d1 = new Dict();
            d1.setPid(dict.getId());
            List<Dict> dictList = dictService.selectByExample(d1);
            List<Integer> integerList = new ArrayList<>();
            integerList.add(id);
            for(Dict dd:dictList){
                integerList.add(dd.getId());
            }
            i = dictService.deleteBatchById(integerList);

        }else{
            i = dictService.deleteByPrimaryKey(id);

        }
        return i;
    }

}
